package com.hsmw.api.controller.sale;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.service.*;
import com.hsmw.common.base.BaseController;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author WD
 */
@Api(tags = "SALE-销售端接口")
@RestController
@RequestMapping("/api/itemPosition")
public class HsmwSaleController extends BaseController<HsmwForcemanEvaluateService, HsmwForcemanAddress> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwForcemanAddressService hsmwForcemanAddressService;

    @Autowired
    private HsmwItemConstructionPositionService hsmwItemConstructionPositionService;

    @Autowired
    private HsmwItemService hsmwItemService;




    /**
     * 分页
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取项目列表，参数： 项目名称 : name", notes = "获取项目列表", produces = "application/json")
    @PostMapping("/getHsmwItemList")
    public DataResponse getHsmwAuditPage(@RequestBody HsmwItem hsmwItem) {
        LambdaQueryWrapper<HsmwItem> lambdaQueryWrapper =new LambdaQueryWrapper<HsmwItem>()
                .eq(HsmwItem::getItemStatus,CommonConstants.NO)
                .orderByDesc(HsmwItem::getCreateTime);
        if (StringUtils.isNotBlank(hsmwItem.getName())){
            lambdaQueryWrapper.like(HsmwItem::getName,hsmwItem.getName());
        }
        List<HsmwItem> data = hsmwItemService.list(lambdaQueryWrapper);
        return DataResponse.success(data);
    }

    /**
     * 分页
     * @param
     * @return
     */
    @ApiOperation(value = "获取项目的 作业位置  列表", notes = "获取项目分页列表  ", produces = "application/json")
    @PostMapping("/getItemAddressList")
    @ApiImplicitParam(name = "itemId",value = "项目ID 必输",dataType = "String")
    public DataResponse getHsmwItemPage(String itemId) {
        List<HsmwForcemanAddress> hsmwForcemanAddresses = hsmwForcemanAddressService.list(new LambdaQueryWrapper<HsmwForcemanAddress>()
                .eq(HsmwForcemanAddress::getItemId,itemId));
        return new DataResponse(hsmwForcemanAddresses);
    }


    /**
     * 分页
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取项目的 施工部位  列表", notes = "获取项目分页列表  ", produces = "application/json")
    @PostMapping("/getItemConsPositionList")
    @ApiImplicitParam(name = "itemId",value = "项目ID 必输",dataType = "String")
    public DataResponse getItemConsPositionList(String itemId) {
        List<HsmwItemConstructionPosition> hsmwItemConstructionPositionList = hsmwItemConstructionPositionService.list(new LambdaQueryWrapper<HsmwItemConstructionPosition>()
                .eq(HsmwItemConstructionPosition::getItemId,itemId));
        return new DataResponse(hsmwItemConstructionPositionList);
    }



    /**
     * 常用地址信息的增删改查
     *
     * @param
     * @return
     */
    @ApiOperation(value = "作业位置 增加  详细地址，地址标题，经度，纬度 不能为空", notes = "常用地址信息的增加", produces = "application/json")
    @PostMapping("/address/add")
    public DataResponse addressAdd(@RequestBody HsmwForcemanAddress hsmwForcemanAddress) {
        try {
            LoginUser currentUser = currentHsmwUserUtils.getCurrentUserInfo();
            if(StringUtils.isBlank(hsmwForcemanAddress.getItemId())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的项目ID为空，请重新输入！");
            }
            if(StringUtils.isBlank(hsmwForcemanAddress.getAddrDetail())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的详细地址为空，请重新输入！");
            }
            if( StringUtils.isBlank(hsmwForcemanAddress.getAddrTitle())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的地址标题为空，请重新输入！");
            }
            if( StringUtils.isBlank(hsmwForcemanAddress.getLng())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的经度为空，请重新输入！");
            }
            if( StringUtils.isBlank(hsmwForcemanAddress.getLat())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的纬度为空，请重新输入！");
            }
            LoginUser hsmwUser = currentHsmwUserUtils.getCurrentUserInfo();
            hsmwForcemanAddress.setUserId(hsmwUser.getId());
            hsmwForcemanAddressService.save(hsmwForcemanAddress);
            return new DataResponse();
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"新增常用地址信息异常，请联系管理员！");
        }
    }


    /**
     * 常用地址信息的增删改查
     *
     * @param
     * @return
     */
    @ApiOperation(value = "作业位置 的修改", notes = "作业位置 信息的修改", produces = "application/json")
    @PostMapping("/address/update")
    public DataResponse updateAddressInfo(@RequestBody HsmwForcemanAddress hsmwForcemanAddress) {
        try {
            LoginUser hsmwUser = currentHsmwUserUtils.getCurrentUserInfo();
            if(StringUtils.isBlank(hsmwForcemanAddress.getId())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的地址ID为空，请重新输入！");
            }

            hsmwForcemanAddress.setUpdateUser(hsmwUser.getId());
            hsmwForcemanAddressService.updateById(hsmwForcemanAddress);
            return new DataResponse();
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"修改常用地址信息异常，请联系管理员！");
        }
    }

    /**
     * 常用地址信息的增删改查
     *
     * @param
     * @return
     */
    @ApiOperation(value = "  作业位置 的删除", notes = "常用地址信息的删除", produces = "application/json")
    @PostMapping("/address/delete")
    public DataResponse deleteAddressInfo(@RequestBody HsmwForcemanAddress hsmwForcemanAddress) {
        try {
            if(StringUtils.isBlank(hsmwForcemanAddress.getId())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的地址ID为空，请重新输入！");
            }
            hsmwForcemanAddressService.removeById(hsmwForcemanAddress.getId());
            return new DataResponse();
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"修改常用地址信息异常，请联系管理员！");
        }
    }





    /**
     * 施工部位的增删改查
     *
     * @param
     * @return
     */
    @ApiOperation(value = "施工部位 增加  部位名称，项目ID 不能为空", notes = "施工部位的增加", produces = "application/json")
    @PostMapping("/position/add")
    public DataResponse positionAdd(@RequestBody HsmwItemConstructionPosition hsmwItemConstructionPosition) {
        try {
            LoginUser currentUser = currentHsmwUserUtils.getCurrentUserInfo();
            if(StringUtils.isBlank(hsmwItemConstructionPosition.getItemId())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的项目ID为空，请重新输入！");
            }
            if( StringUtils.isBlank(hsmwItemConstructionPosition.getPosition())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的施工部位为空，请重新输入！");
            }
            String addressId = CommonUtils.generateId(hsmwItemConstructionPosition);
            hsmwItemConstructionPosition.setId(addressId);
            hsmwItemConstructionPosition.setCreateUser(currentUser.getId());
            hsmwItemConstructionPositionService.save(hsmwItemConstructionPosition);
            HashMap result = new HashMap();
            result.put("positionId",addressId);
            return new DataResponse(result);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"新增施工部位信息异常，请联系管理员！");
        }
    }


    /**
     * 施工部位
     *
     * @param
     * @return
     */
    @ApiOperation(value = "施工部位 的修改", notes = "作业位置 信息的修改", produces = "application/json")
    @PostMapping("/position/update")
    public DataResponse updatePositionInfo(@RequestBody HsmwItemConstructionPosition hsmwItemConstructionPosition) {
        try {
            LoginUser hsmwUser = currentHsmwUserUtils.getCurrentUserInfo();
            if(StringUtils.isBlank(hsmwItemConstructionPosition.getId())){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的施工部位 ID为空，请重新输入！");
            }
            hsmwItemConstructionPosition.setUpdateUser(hsmwUser.getId());
            hsmwItemConstructionPositionService.updateById(hsmwItemConstructionPosition);
            return new DataResponse();
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"修改施工部位异常，请联系管理员！");
        }
    }
    /**
     * 施工部位
     *
     * @param
     * @return
     */
    @ApiOperation(value = "  施工部位 的删除", notes = "常用地址信息的删除", produces = "application/json")
    @PostMapping("/position/delete")
    public DataResponse deletePositionInfo(String positionId) {
        try {
            if(StringUtils.isBlank(positionId)){
                return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, "您输入的施工位置 ID为空，请重新输入！");
            }
            hsmwItemConstructionPositionService.removeById(positionId);
            return new DataResponse();
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"施工部位信息删除异常，请联系管理员！");
        }
    }









}
